{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-right">
        <button id="button-send" data-loading-text="{{ text_loading }}" data-toggle="tooltip" title="{{ button_send }}" class="btn btn-primary" onclick="send('index.php?route=marketing/contact/send&user_token={{ user_token }}');"><i class="fas fa-envelope"></i></button>
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-light"><i class="fas fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="card">
      <div class="card-header"><i class="fas fa-envelope"></i> {{ heading_title }}</div>
      <div class="card-body">
        <form>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-store">{{ entry_store }}</label>
            <div class="col-sm-10">
              <select name="store_id" id="input-store" class="form-control">
                <option value="0">{{ text_default }}</option>
                {% for store in stores %}
                  <option value="{{ store.store_id }}">{{ store.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-to">{{ entry_to }}</label>
            <div class="col-sm-10">
              <select name="to" id="input-to" class="form-control">
                <option value="newsletter">{{ text_newsletter }}</option>
                <option value="customer_all">{{ text_customer_all }}</option>
                <option value="customer_group">{{ text_customer_group }}</option>
                <option value="customer">{{ text_customer }}</option>
                <option value="affiliate_all">{{ text_affiliate_all }}</option>
                <option value="affiliate">{{ text_affiliate }}</option>
                <option value="product">{{ text_product }}</option>
              </select>
            </div>
          </div>
          <div class="form-group row to" id="to-customer-group">
            <label class="col-sm-2 col-form-label" for="input-customer-group">{{ entry_customer_group }}</label>
            <div class="col-sm-10">
              <select name="customer_group_id" id="input-customer-group" class="form-control">
                {% for customer_group in customer_groups %}
                  <option value="{{ customer_group.customer_group_id }}">{{ customer_group.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="form-group row to" id="to-customer">
            <label class="col-sm-2 col-form-label" for="input-customer">{{ entry_customer }}</label>
            <div class="col-sm-10">
              <input type="text" name="customers" value="" placeholder="{{ entry_customer }}" id="input-customer" class="form-control"/>
              <div id="mail-customer" class="form-control" style="height: 150px; overflow: auto;"></div>
              <small class="form-text text-muted">{{ help_customer }}</small>
            </div>
          </div>
          <div class="form-group row to" id="to-affiliate">
            <label class="col-sm-2 col-form-label" for="input-affiliate">{{ entry_affiliate }}</label>
            <div class="col-sm-10">
              <input type="text" name="affiliates" value="" placeholder="{{ entry_affiliate }}" id="input-affiliate" class="form-control"/>
              <div id="mail-affiliate" class="form-control" style="height: 150px; overflow: auto;"></div>
              <small class="form-text text-muted">{{ help_affiliate }}</small>
            </div>
          </div>
          <div class="form-group row to" id="to-product">
            <label class="col-sm-2 col-form-label" for="input-product">{{ entry_product }}</label>
            <div class="col-sm-10">
              <input type="text" name="products" value="" placeholder="{{ entry_product }}" id="input-product" class="form-control"/>
              <div id="mail-product" class="form-control" style="height: 150px; overflow: auto;"></div>
              <small class="form-text text-muted">{{ help_product }}</small>
            </div>
          </div>
          <div class="form-group row required">
            <label class="col-sm-2 col-form-label" for="input-subject">{{ entry_subject }}</label>
            <div class="col-sm-10">
              <input type="text" name="subject" value="" placeholder="{{ entry_subject }}" id="input-subject" class="form-control"/>
            </div>
          </div>
          <div class="form-group row required">
            <label class="col-sm-2 col-form-label" for="input-message">{{ entry_message }}</label>
            <div class="col-sm-10">
              <textarea name="message" placeholder="{{ entry_message }}" id="input-message" data-toggle="ckeditor" data-lang="{{ ckeditor }}" class="form-control"></textarea>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('textarea[data-toggle=\'ckeditor\']').ckeditor();

$('select[name=\'to\']').on('change', function() {
	$('.to').hide();

	$('#to-' + this.value.replace('_', '-')).show();
});

$('select[name=\'to\']').trigger('change');
//--></script>
<script type="text/javascript"><!--
// Customers
$('input[name=\'customers\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=customer/customer/autocomplete&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['customer_id']
					}
				}));
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	},
	'select': function(item) {
		$('input[name=\'customers\']').val('');

		$('#mail-customer' + item['value']).remove();

		$('#mail-customer').append('<div id="mail-customer' + item['value'] + '"><i class="fas fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="customer[]" value="' + item['value'] + '" /></div>');
	}
});

$('#mail-customer').on('click', '.fa-minus-circle', function() {
	$(this).parent().remove();
});

// Affiliates
$('input[name=\'affiliates\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=customer/customer/autocomplete&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request) + '&filter_affiliate=1',
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['customer_id']
					}
				}));
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	},
	'select': function(item) {
		$('input[name=\'affiliates\']').val('');

		$('#mail-affiliate' + item['value']).remove();

		$('#mail-affiliate').append('<div id="mail-affiliate' + item['value'] + '"><i class="fas fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="affiliate[]" value="' + item['value'] + '" /></div>');
	}
});

$('#input-affiliate').on('click', '.fa-minus-circle', function() {
	$(this).parent().remove();
});

// Products
$('input[name=\'products\']').autocomplete({
	'source': function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/product/autocomplete&user_token={{ user_token }}&filter_name=' + encodeURIComponent(request),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item['name'],
						value: item['product_id']
					}
				}));
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	},
	'select': function(item) {
		$('input[name=\'products\']').val('');

		$('#mail-product' + item['value']).remove();

		$('#mail-product').append('<div id="mail-product' + item['value'] + '"><i class="fas fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="product[]" value="' + item['value'] + '" /></div>');
	}
});

$('#mail-product').on('click', '.fa-minus-circle', function() {
	$(this).parent().remove();
});

function send(url) {
	$.ajax({
		url: url,
		type: 'post',
		data: $('#content select, #content input, #content textarea'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-send').button('loading');
		},
		complete: function() {
			$('#button-send').button('reset');
		},
		success: function(json) {
			$('.alert-dismissible, .invalid-tooltip').remove();

			if (json['error']) {
				if (json['error']['warning']) {
					$('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['warning'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}

				if (json['error']['email']) {
					$('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error']['email'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}

				if (json['error']['subject']) {
					$('input[name=\'subject\']').after('<div class="invalid-tooltip d-block">' + json['error']['subject'] + '</div>');
				}

				if (json['error']['message']) {
					$('textarea[name=\'message\']').after('<div class="invalid-tooltip d-block">' + json['error']['message'] + '</div>');
				}
			}

			if (json['success']) {
				$('#content > .container-fluid').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i>  ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
			}

			if (json['next']) {
				send(json['next']);
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
}

//--></script>
{{ footer }}